<template>
  <div style="border: 1px solid #ccc">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden;"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
        @onChange="handleChange"
      />
    </div>
</template>
<script setup>
import { ref, shallowRef, onBeforeUnmount, onMounted } from "vue";
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import '@wangeditor/editor/dist/css/style.css'
// 接收父组件传过来的值
const props = defineProps({
  content:{
    type:String
  }
})
const emits = defineEmits(['event'])
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()
// 内容 HTML
const valueHtml = ref('<p></p>')

const editor = editorRef.value
onMounted(()=>{
  if (editor == null) return
  props.content && editor.setHtml(props.content)
})
// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    if (editor == null) return
    editor.destroy()
})
const handleCreated = (editor) => {
  editorRef.value = editor
}
// 将子组件的内容传出去
const handleChange = () =>{
  emits('event',valueHtml)
}
</script>
<style lang="scss" scoped>
</style>